package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;


    /**
     * 套餐分页查询功能
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmealPageQueryDTO,setmealDTO);
        List<Setmeal> list = setmealMapper.page(setmealDTO);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(list);
        List<SetmealVO> voList = list.stream().map(setmeal -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal, setmealVO);
            Category category = categoryMapper.findById(setmeal.getCategoryId());
            setmealVO.setCategoryName(category.getName());
            return setmealVO;
        }).collect(Collectors.toList());
        return new PageResult(pageInfo.getTotal(), voList);
    }

    /**
     * 新增套餐功能
     *
     * @param setmealDTO
     * 业务分析
     *     1.增加套餐到setmeal表,并且要获取主键
     *     2.将获取的主键id设置成setmealId
     *     3.增加套餐菜品到setmeal_dish表
     */
    @Transactional
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //先增加套餐,并获取主键
        setmealMapper.add(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            //将获取的setmeal主键id设置成SetmealId保存到setmeal_dish表
            setmealDish.setSetmealId(setmeal.getId());
            //增加套餐菜品到setmeal_dish表
            setmealDishMapper.add(setmealDish);
        });
    }

    /**
     * 删除套餐功能
     *
     * @param ids
     * 业务分析:
     *     1.在售中的套餐不能直接删除
     *     2.删除套餐
     *     3.删除对应的套餐菜品表的里的菜品
     */
    @Transactional
    @Override
    public void delByIds(List<Long> ids) {
        //做一个参数健壮性判断
        if (ids == null || ids.size() == 0){
            throw new DeletionNotAllowedException(MessageConstant.DELETE_LIST_HAVE_NO_CONTENT);
        }
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.findById(id);
            //1.在售中的套餐不能直接删除
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //删除套餐
        ids.forEach(id -> setmealMapper.delById(id));
        //删除套餐菜品
        ids.forEach(id -> setmealDishMapper.delBySetmealId(id));
    }

    /**
     * 修改套餐功能
     *
     * @param setmealDTO
     * 业务分析:
     *     1.先修改套餐信息
     *     2.删除所有对应的套餐菜品
     *     3.重新添加新的套餐菜品
     *     4.开启事务
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改套餐
        setmealMapper.update(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //删除之前的套餐菜品
        setmealDishMapper.delBySetmealId(setmealDTO.getId());
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
            //重新添加新的套餐菜品
            setmealDishMapper.add(setmealDish);
        });
    }

    /**
     * 根据id查询套餐功能
     *
     * @param id
     * @return
     * 业务分析:
     * 1.先根据id查询套餐
     * 2.根据setmealId查询套餐菜品
     * 3.将上面的内容封装到一个VO中
     */
    @Override
    public SetmealVO findById(Long id) {
        //根据id查询套餐
        Setmeal setmeal = setmealMapper.findById(id);
        //根据setmealId查询套餐菜品
        List<SetmealDish> list = setmealDishMapper.findBySetmealId(id);
        //3.将上面的内容封装到一个VO中
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /**
     * 启售或停售套餐功能
     *
     * @param status
     * @param id
     * 业务分析:
     *     1.查询套餐中的菜品状态,如果有停售的,这个套餐就不能启售,直接抛出业务异常
     *     2.如果套餐中的菜品都在启售状态,就可以直接修改状态
     *
     */
    @Override
    public void enableOrDisable(Integer status, Long id) {
        //如果是启售的话,才做业务逻辑判断
        if (status == StatusConstant.ENABLE) {
            List<SetmealDish> list = setmealDishMapper.findBySetmealId(id);
            list.forEach(SetmealDish -> {
                //根据dishId查询菜品
                Dish dish = dishMapper.findById(SetmealDish.getDishId());
                //如果菜品中有未启售的,直接抛出业务异常
                if (dish.getStatus() == StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }

        //如果没有上述情况,就可以直接更改状态
        Setmeal setmeal = new Setmeal();

        setmeal.setId(id);
        setmeal.setStatus(status);
        //调用修改套餐的接口
        setmealMapper.update(setmeal);
    }

    /**
     * 根据套餐id查询在售中的套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public List<Setmeal> list(SetmealDTO setmealDTO) {
        return setmealMapper.page(setmealDTO);
    }

    /**
     * 根据套餐id查询包含的菜品列表
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        //根据套餐id查询绑定的所有菜品关系
        List<SetmealDish> setmealDishes = setmealDishMapper.findBySetmealId(id);

        List<DishItemVO> dishItemList = setmealDishes.stream()
                .map(setmealDish -> {
                    Long dishId = setmealDish.getDishId();
                    //根据菜品id查询菜品信息
                    Dish dish = dishMapper.findById(dishId);
                    //封装DishItem
                    DishItemVO dishItemVO = new DishItemVO();
                    dishItemVO.setImage(dish.getImage());
                    dishItemVO.setDescription(dish.getDescription());
                    dishItemVO.setCopies(setmealDish.getCopies());
                    dishItemVO.setName(setmealDish.getName());
                    return dishItemVO;
                }).collect(Collectors.toList());

        return dishItemList;
    }
}
